package cn.com.b2office.struts2.action;

import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import cn.com.b2office.domain.Order;
import cn.com.b2office.domain.User;
import cn.com.b2office.service.OrderService;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author zhenggang
 * @version Jul 8, 2008 5:22:32 PM comment
 */
public class ListMemberOrderAction extends ActionSupport implements
		SessionAware {

	private static final long serialVersionUID = -8444595566066529344L;

	private Map session = null;

	private OrderService orderService;

	// private String memberId = "W0806209";
	private String memberId;

	private int orderStatus = 5; // 订单状态 (默认为5，全部取出)

	private int totalRecordNum; // 总记录数

	private int totalPageNum = 0; // 一共多少页

	private int curPageNum = 1; // 当前页码数 (默认为1)

	private int maxResults = 10; // 每页显示记录数

	private int firstResult = 0;

	private List<Order> listOrder;

	public String execute() throws Exception {
		User user = (User) session.get("user");
		if (user == null)
			return "login";
		memberId = user.getMemberId();
		firstResult = (curPageNum - 1) * maxResults;
		listOrder = orderService.findOrders(memberId, orderStatus, firstResult,
				maxResults);
		totalRecordNum = orderService.getTotalRecordNumByOrderStatus(memberId,
				orderStatus);

		if (totalRecordNum < maxResults) {
			totalPageNum = 1;
		} else if (totalRecordNum % maxResults == 0) {
			totalPageNum = totalRecordNum / maxResults;
		} else {
			totalPageNum = totalRecordNum / maxResults + 1;
		}

		return SUCCESS;
	}

	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}

	public void setMemberId(String memberId) {
		this.memberId = memberId;
	}

	public void setFirstResult(int firstResult) {
		this.firstResult = firstResult;
	}

	public void setMaxResults(int maxResults) {
		this.maxResults = maxResults;
	}

	public List<Order> getListOrder() {
		return listOrder;
	}

	public int getTotalRecordNum() {
		return totalRecordNum;
	}

	public int getTotalPageNum() {
		return totalPageNum;
	}

	public int getCurPageNum() {
		return curPageNum;
	}

	public int getOrderStatus() {
		return orderStatus;
	}

	public void setOrderStatus(int orderStatus) {
		this.orderStatus = orderStatus;
	}

	public void setCurPageNum(int curPageNum) {
		this.curPageNum = curPageNum;
	}

	public void setSession(Map session) {
		this.session = session;
	}
}
